home *** CD-ROM | disk | FTP | other *** search
/ PC World 2004 February / PCWorld_2004-02_cd.bin / software / vyzkuste / inno / isetup-4.0.10.exe / {app} / Examples / CodeDlg.iss < prev    next >
Text File  |  2003-04-14  |  11KB  |  250 lines

  1. ; -- CodeDlg.iss --
  2. ;
  3. ; This script shows how to insert custom wizard pages into Setup and how to handle
  4. ; navigation between those pages if multiple custom pages are inserted after each
  5. ; other. Furthermore it shows how to 'communicate' between the [Code] section and
  6. ; the regular Inno Setup sections using {code:...} constants and finally it shows
  7. ; how to customize the settings text on the 'Ready To Install' page.
  8.  
  9. [Setup]
  10. AppName=My Program
  11. AppVerName=My Program version 1.5
  12. DefaultDirName={pf}\My Program
  13. DisableProgramGroupPage=yes
  14. UninstallDisplayIcon={app}\MyProg.exe
  15.  
  16. [Files]
  17. Source: "MyProg.exe"; DestDir: "{app}"
  18. Source: "MyProg.hlp"; DestDir: "{app}"
  19. Source: "Readme.txt"; DestDir: "{app}"; Flags: isreadme
  20.  
  21. [Registry]
  22. Root: HKCU; Subkey: "Software\My Company"; Flags: uninsdeletekeyifempty
  23. Root: HKCU; Subkey: "Software\My Company\My Program"; Flags: uninsdeletekey
  24. Root: HKCU; Subkey: "Software\My Company\My Program\Settings"; ValueType: string; ValueName: "Name"; ValueData: "{code:GetUser|Name}"
  25. Root: HKCU; Subkey: "Software\My Company\My Program\Settings"; ValueType: string; ValueName: "Company"; ValueData: "{code:GetUser|Company}"
  26. Root: HKCU; Subkey: "Software\My Company\My Program\Settings"; ValueType: string; ValueName: "DataDir"; ValueData: "{code:GetDataDir}"
  27. ; etc.
  28.  
  29. [Dirs]
  30. Name: {code:GetDataDir}; Flags: uninsneveruninstall
  31.  
  32. [Code]
  33. var
  34.   UserPrompts, UserValues: TArrayOfString;
  35.   UsagePrompts, UsageValues: TArrayOfString;
  36.   Key: String;
  37.   DataDir: String;
  38.   
  39. function InitializeSetup(): Boolean;
  40. begin
  41.   { Set prompts used on custom wizard pages }
  42.   SetArrayLength(UserPrompts, 2);
  43.   UserPrompts[0] := 'Name:';
  44.   UserPrompts[1] := 'Company:';
  45.  
  46.   SetArrayLength(UsagePrompts, 3)
  47.   UsagePrompts[0] := 'Light mode (no ads, limited functionality)';
  48.   UsagePrompts[1] := 'Sponsored mode (with ads, full functionality)';
  49.   UsagePrompts[2] := 'Paid mode (no ads, full functionality)';
  50.  
  51.   { Set default values }
  52.   SetArrayLength(UserValues, 2);
  53.   RegQueryStringValue(HKLM, 'Software\Microsoft\Windows\CurrentVersion', 'RegisteredOwner', UserValues[0]);
  54.   RegQueryStringValue(HKLM, 'Software\Microsoft\Windows\CurrentVersion', 'RegisteredOrganization', UserValues[1]);
  55.   if (UserValues[0] = '') and (UserValues[1] = '') then begin
  56.     RegQueryStringValue(HKCU, 'Software\Microsoft\MS Setup (ACME)\User Info', 'DefName', UserValues[0]);
  57.     RegQueryStringValue(HKCU, 'Software\Microsoft\MS Setup (ACME)\User Info', 'DefCompany', UserValues[1]);
  58.   end;
  59.  
  60.   SetArrayLength(UsageValues, 3)
  61.   UsageValues[1] := '1';
  62.  
  63.   { Try to find the settings that were stored last time (also see below). }
  64.   UserValues[0] := GetPreviousData('Name', UserValues[0]);
  65.   UserValues[1] := GetPreviousData('Company', UserValues[1]);
  66.   UsageValues[0] := GetPreviousData('Light mode', UsageValues[0]);
  67.   UsageValues[1] := GetPreviousData('Sponsored mode', UsageValues[1]);
  68.   UsageValues[2] := GetPreviousData('Paid mode', UsageValues[2]);
  69.   DataDir := GetPreviousData('DataDir', '');
  70.  
  71.   { Let Setup run }
  72.   Result := True;
  73. end;
  74.  
  75. procedure RegisterPreviousData(PreviousDataKey: Integer);
  76. begin
  77.   { Store the settings so we can restore them next time }
  78.   SetPreviousData(PreviousDataKey, 'Name', UserValues[0]);
  79.   SetPreviousData(PreviousDataKey, 'Company', UserValues[1]);
  80.   SetPreviousData(PreviousDataKey, 'Light mode', UsageValues[0]);
  81.   SetPreviousData(PreviousDataKey, 'Sponsored mode', UsageValues[1]);
  82.   SetPreviousData(PreviousDataKey, 'Paid mode', UsageValues[2]);
  83.   SetPreviousData(PreviousDataKey, 'DataDir', DataDir);
  84. end;
  85.  
  86. function ScriptDlgPages(CurPage: Integer; BackClicked: Boolean): Boolean;
  87. var
  88.   I, CurSubPage: Integer;
  89.   Next, NextOk: Boolean;
  90. begin
  91.   if (not BackClicked and (CurPage = wpSelectDir)) or (BackClicked and (CurPage = wpReady)) then begin
  92.     { Insert a custom wizard page between two non custom pages }
  93.     { First open the custom wizard page }
  94.     ScriptDlgPageOpen();
  95.     { Set some captions }
  96.     ScriptDlgPageSetCaption('Select Personal Data Directory');
  97.     ScriptDlgPageSetSubCaption1('Where should personal data files be installed?');
  98.     ScriptDlgPageSetSubCaption2('Select the folder you would like Setup to install personal data files to, then click Next.');
  99.     { Initialize the DataDir if necessary }
  100.     if DataDir = '' then
  101.       DataDir := 'C:\'+UserValues[0];
  102.     { Ask for a dir until the user has entered one or click Back or Cancel }
  103.     Next := InputDir(UserValues[0], DataDir);
  104.     while Next and (DataDir = '') do begin
  105.       MsgBox(SetupMessage(msgInvalidPath), mbError, MB_OK);
  106.       Next := InputDir(UserValues[0], DataDir);
  107.     end;
  108.     { See NextButtonClick and BackButtonClick: return True if the click should be allowed }
  109.     if not BackClicked then
  110.       Result := Next
  111.     else
  112.       Result := not Next;
  113.     { Close the wizard page. Do a FullRestore only if the click (see above) is not allowed }
  114.     ScriptDlgPageClose(not Result);
  115.   end else if (not BackClicked and (CurPage = wpWelcome)) or (BackClicked and (CurPage = wpSelectDir)) then begin
  116.     { Insert multiple custom wizard page between two non custom pages }
  117.     { Now we must handle navigation between the custom pages ourselves }
  118.     { First find out on which page we should start }
  119.     if not BackClicked then
  120.       CurSubPage := 0
  121.     else
  122.       CurSubPage := 2;
  123.     { Then open the custom wizard page }
  124.     ScriptDlgPageOpen();
  125.     { Set the main caption }
  126.     ScriptDlgPageSetCaption('Personal Information');
  127.     { Loop while we are still on a custom page and Setup has not been terminated }
  128.     while (CurSubPage >= 0) and (CurSubPage <= 2) and not Terminated do begin
  129.       case CurSubPage of
  130.         0:
  131.           begin
  132.             { First ask for some user info }
  133.             ScriptDlgPageSetSubCaption1('Who are you?');
  134.             ScriptDlgPageSetSubCaption2('Please specify your name and the company for whom you work, then click Next.');
  135.             Next := InputQueryArray(UserPrompts, UserValues);
  136.             if Next then begin
  137.               NextOk := UserValues[0] <> '';
  138.               if not NextOk then
  139.                 MsgBox('You must enter your name.', mbError, MB_OK);
  140.             end;
  141.           end;
  142.         1:
  143.           begin
  144.             { Then ask for the usage mode }
  145.             ScriptDlgPageSetSubCaption1('How will you use My Progam?');
  146.             ScriptDlgPageSetSubCaption2('Please specify how you would like to use My Program, then click Next.');
  147.             Next := InputOptionArray(UsagePrompts, UsageValues, True, False);
  148.             NextOk := True;
  149.           end;
  150.         2:
  151.           begin
  152.             if UsageValues[0] = '1' then begin
  153.               { Show a message if 'light mode' was chosen above }
  154.               { Skip this page when the user just clicked the Back button }
  155.               ScriptDlgPageSetSubCaption1('How will you use My Progam?');
  156.               Next := OutputMsg('Note: to enjoy all features My Program can offer and to support its development, you can switch to sponsored or paid mode at any time by selecting ''Usage Mode'' in the ''Help'' menu of My Program after the installation has completed.'#13#13'Click Back if you want to change your usage mode setting now, or click Next to continue with the installation.', True);
  157.               NextOk := True;
  158.             end else if UsageValues[2] = '1' then begin
  159.               { Ask for a registration key if 'paid mode' was chosen above }
  160.               { This demo accepts only one key: 'isx' (without quotes) }
  161.               ScriptDlgPageSetSubCaption1('What''s your registration key?');
  162.               ScriptDlgPageSetSubCaption2('Please specify your registration key, ' + UserValues[0] + '. Click Next to continue. If you don''t have a valid registration key, click Back to choose a different usage mode.');
  163.               Next := InputQuery('Registration key:', Key);
  164.               if Next then begin
  165.                 { Just to show how OutputProgress works }
  166.                 for I := 0 to 10 do begin
  167.                   OutputProgress('Authorizing registration key...', '', I, 10);
  168.                   Sleep(100);
  169.                   if Terminated then
  170.                     Break;
  171.                 end;
  172.                 NextOk := Key = 'isx';
  173.                 if not NextOk and not Terminated then
  174.                   MsgBox('You must enter a valid registration key.', mbError, MB_OK);
  175.               end;
  176.             end;
  177.           end;
  178.       end;
  179.       if Next then begin
  180.         { Go to the next page, but only if the user entered correct information }
  181.         if NextOk then
  182.           CurSubPage := CurSubPage + 1;
  183.       end else
  184.         CurSubPage := CurSubPage - 1;
  185.     end;
  186.     { See NextButtonClick and BackButtonClick: return True if the click should be allowed }
  187.     if not BackClicked then
  188.       Result := Next
  189.     else
  190.       Result := not Next;
  191.     { Close the wizard page. Do a FullRestore only if the click (see above) is not allowed }
  192.     ScriptDlgPageClose(not Result);
  193.   end else begin
  194.     Result := True;
  195.   end;
  196. end;
  197.  
  198. function NextButtonClick(CurPage: Integer): Boolean;
  199. begin
  200.   Result := ScriptDlgPages(CurPage, False);
  201. end;
  202.  
  203. function BackButtonClick(CurPage: Integer): Boolean;
  204. begin
  205.   Result := ScriptDlgPages(CurPage, True);
  206. end;
  207.  
  208. function UpdateReadyMemo(Space, NewLine, MemoUserInfoInfo, MemoDirInfo, MemoTypeInfo, MemoComponentsInfo, MemoGroupInfo, MemoTasksInfo: String): String;
  209. var
  210.   S: String;
  211. begin
  212.   { Fill the 'Ready Memo' with the normal settings and the custom settings }
  213.   S := '';
  214.   S := S + 'Personal Information:' + NewLine;
  215.   S := S + Space + UserValues[0] + NewLine;
  216.   if UserValues[1] <> '' then
  217.     S := S + Space + UserValues[1] + NewLine;
  218.   S := S + NewLine;
  219.   
  220.   S := S + 'Usage Mode:' + NewLine;
  221.   if UsageValues[0] = '1' then
  222.     S := S + Space + UsagePrompts[0] + NewLine
  223.   else if UsageValues[1] = '1' then
  224.     S := S + Space + UsagePrompts[1] + NewLine
  225.   else
  226.     S := S + Space + UsagePrompts[2] + NewLine;
  227.   S := S + NewLine;
  228.   
  229.   S := S + MemoDirInfo + NewLine;
  230.   S := S + Space + DataDir + ' (personal data files)' + NewLine;
  231.  
  232.   Result := S;
  233. end;
  234.  
  235. function GetUser(S: String): String;
  236. begin
  237.   { Return a user value }
  238.   { Could also be splitted into separate GetUserName and GetUserCompany functions }
  239.   if S = 'Name' then
  240.     Result := UserValues[0]
  241.   else if S = 'Company' then
  242.     Result := UserValues[1];
  243. end;
  244.  
  245. function GetDataDir(S: String): String;
  246. begin
  247.   { Return the selected DataDir }
  248.   Result := DataDir;
  249. end;
  250.